package cn.zhizihua.service.impl;

import cn.zhizihua.mapper.FoodMapper;
import cn.zhizihua.pojo.Food;
import cn.zhizihua.query.BaseQuery;
import cn.zhizihua.query.FoodQuery;
import cn.zhizihua.service.IFoodService;
import cn.zhizihua.vo.PageBean;
import cn.zhizihua.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  1、添加@Service 注解
 *  2、装配Mapper层对象
 *  3、进行方法参数校验  事务处理@Transactional
 */
@Service
public class FoodService implements IFoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Transactional
    @Override
    public ResultBean add(Food food) {
        // 参数校验
        if (food == null || StringUtils.isEmpty(food.getFoodName())){
            return ResultBean.ERROR("foodService-add 请求参数为空");
        }
        int i = foodMapper.add(food);
        return i>0?ResultBean.SUCCESS(food.getFoodId()):ResultBean.ERROR("foodService-add 添加餐品失败");
    }

    @Transactional
    @Override
    public ResultBean update(Food food) {
        // 参数校验
        if (food == null ||
                food.getFoodId() == null|| food.getFoodId()==0||
                StringUtils.isEmpty(food.getFoodName())){
            return ResultBean.ERROR("foodService-update 请求参数为空");
        }
        int i = foodMapper.update(food);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("foodService-update 修改餐品失败");
    }

    @Transactional
    @Override
    public ResultBean delete(Integer id) {
        // 参数校验
        if (id == null){
            return ResultBean.ERROR(10001,"foodService-delete 请求参数为空");
        }
        int i = foodMapper.delete(id);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("foodService-delete 删除餐品失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {
        // 参数校验
        if (id == null){
            return ResultBean.ERROR("foodService-getById 请求参数为空");
        }
        Food byId = foodMapper.getById(id);
        return byId!=null?ResultBean.SUCCESS(byId):ResultBean.ERROR("foodService-getById 根据id查询餐品失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<Food> foodList = foodMapper.getAll();
        return foodList!=null?ResultBean.SUCCESS(foodList):ResultBean.ERROR("foodService-getAll 获取全部餐品失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {
        if (baseQuery == null){
            baseQuery = new FoodQuery();
        }

        PageBean<Food> pageBean = new PageBean<>();
        int count = foodMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);  //总记录数

        List<Food> foodList = foodMapper.getByPage(baseQuery);
        pageBean.setData(foodList);  //当前页数据

        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids){
        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("foodService-batchDel 批量删除ids为空");
        }

        for (Integer id:ids){
            int delete = foodMapper.delete(id);
            if (delete < 1){
                throw new RuntimeException("foodService-batchDel 批量删除餐品失败");
            }
        }

        return ResultBean.SUCCESS();
    }

    /*
   根据商家id查询餐品
    */
    @Override
    public ResultBean getByBusinessId(Integer businessId) {
        if (businessId==null || businessId==0){
            return ResultBean.ERROR("请求参数为空");
        }

        List<Food> foodList = foodMapper.getByBusinessId(businessId);

        return ResultBean.SUCCESS(foodList);
    }
}
